// pages/comment/comment.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: 1,
    thumbUrl: getApp().globalData.thumbUrl,
    allCount: 0,
    goodCom: 0,
    havePic: 0,
    negativeCom: 0,
    page:1,
    show:1

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var id = options.id;
    var uid = wx.getStorageSync('uid');
    var param = {
      id: id,
      type: type,
      uid: uid,
      show:1
    }
    app.wxRequest('POST', "Index/getCommentList", param, (res) => {
      var data = res.data;
      that.setData({
        allCount: data.allCount,
        goodCom: data.goodCom,
        havePic: data.havePic,
        negativeCom: data.negativeCom,
        list: data.list,
        id: id,
        type: type
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  showTab: function (e) {
    var that = this;
    var show = e.currentTarget.dataset.type;
    var id = that.data.id;
    var type=that.data.type;
    that.setData({
      show: show,
      page:1
    })
    var uid = wx.getStorageSync('uid');
    var param = {
      id: id,
      type: type,
      uid: uid,
      show: show
    };
    app.wxRequest('POST', "Index/getCommentList", param, (res) => {
      var data = res.data;
      that.setData({
        list: data.list
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  doZanData: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var list = that.data.list;
    var uid = wx.getStorageSync('uid');
    var param = {
      cid: id,
      uid: uid
    };
    app.wxRequest('POST', "Index/clickAgree", param, (res) => {
      that.setData({
        list: data.list
      })
    }, (err) => {
      console.log(err.errMsg)
    })
    for (var i in list) {
      if (list[i].id == id) {
        if (list[i].status == 1) {
          list[i].status = 0;
        } else if (list[i].status == 0) {
          list[i].status = 1;
        }
      }
    }
    that.setData({
      list: list
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  previewImage: function(e) { 
    var current = e.currentTarget.dataset.url;
    wx.previewImage({
      current: current,
      urls: [current]
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    var that = this;
    var type = that.data.type;
    var id = that.data.id;
    var uid = wx.getStorageSync('uid');
    var show=that.data.show;
    var param = {
      id: id,
      type: type,
      uid: uid,
      show:show
    }
    app.wxRequest('POST', "Index/getCommentList", param, (res) => {
      var data = res.data;
      that.setData({
        allCount: data.allCount,
        goodCom: data.goodCom,
        havePic: data.havePic,
        negativeCom: data.negativeCom,
        list: data.list
      })
    }, (err) => {
      console.log(err.errMsg)
    })
    setTimeout(function(){
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    },3000)
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var type = that.data.type;
    var id = that.data.id;
    var page =that.data.page;
    var uid = wx.getStorageSync('uid');
    var newPage=parseInt(page) + 1;
    var list=that.data.list;
    var show=that.data.show;
    var param = {
      id: id,
      type: type,
      uid: uid,
      show:show,
      page:newPage
    }
    app.wxRequest('POST', "Index/getMoreCommentList", param, (res) => {
      var data = res.data;
      var code=res.code;
      if(code == 1){
        that.setData({
          list: list.concat(data),
          page:newPage
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none",
          duration:1500
        })
      }
      
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})